<?php

namespace App\Http\Controllers\WebPage;

/**
 * 返回视频
 */

use App\Http\Controllers\Controller;
use App\Http\Models\Video;
use App\Http\Models\VideoType;
use App\Http\Requests\WebPage\Video\VideoIndexRequest;
use App\Services\Utils\ApiChecker;
use App\Services\Utils\ApiResponser;

class VideoController extends Controller
{
    use ApiResponser, ApiChecker;

    const DEFAULT_PER_PAGE = 4;

    const LATEST_COLUMN = 'updated_at';

    /**
     * 返回视频列表
     *
     *
     * @param VideoIndexRequest $request
     * @return array
     */
    public function index(VideoIndexRequest $request)
    {
        $videos_paginate = Video::select($this->videoIndexSelectColumns())
            ->VideoTypeId($request->video_type_id)
            ->orderBy('sort','DESC')
            ->latest(self::LATEST_COLUMN)
            ->paginate($request->per_page ?: self::DEFAULT_PER_PAGE);

        return $this->response(compact('videos_paginate'));
    }

    /**
     * @return array
     */
    private function videoIndexSelectColumns()
    {
        return [
            'id' , 'name', 'url', 'created_at','preview_img','english_title'
        ];
    }

    public function create()
    {

        $all = [['id'=>0,'name'=>'全部']];

        $types = VideoType::select('id','name')->get()->toArray();

        $types = array_values(array_merge($all,$types));

        return $this->response(compact('types'));
    }

}
